package edu.dlust.trading_platform;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import edu.dlust.trading_platform.DataBean.hostname;

public class WelcomeActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        login();
    }
    public void login() {
        new Thread() {
            @Override
            public void run() {
                ExecutorService executor = Executors.newSingleThreadExecutor();
                Future<?> future = executor.submit(() -> {
                    try {
                        Thread.sleep(10000); // 等待10秒
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(WelcomeActivity.this, "连接服务器失败", Toast.LENGTH_LONG).show();
                            }
                        });
                        go();
                    } catch (InterruptedException e) {
                    }
                });
                String url = "jdbc:mysql://frp.lsakura.top:8001/android";
                String user = "local";
                String password = "123456";

                try {
                    // 加载数据库驱动
                    Class.forName("com.mysql.jdbc.Driver");
                    // 创建数据库连接
                    Connection connection = DriverManager.getConnection(url, user, password);
                    // 关闭连接
                    if (connection != null && !connection.isClosed()) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // 在主线程中显示Toast消息
                                Toast.makeText(WelcomeActivity.this, "连接服务器成功", Toast.LENGTH_LONG).show();
                            }
                        });
                        System.out.println("数据库连接成功！");
                        // 进行其他数据库操作
                    } else {
                        System.out.println("数据库连接失败！");
                    }

                    // 关闭连接
                    if (connection != null) {
                        connection.close();
                    }
                } catch (SQLException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // 在主线程中显示Toast消息
                            Toast.makeText(WelcomeActivity.this, "连接服务器失败", Toast.LENGTH_LONG).show();
                        }
                    });
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);//尝试加载驱动错误
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                future.cancel(true);
                executor.shutdown();
                go();
            }
        }.start();
    }
    private void go(){
        Intent intent = new Intent(WelcomeActivity.this,LoginActivity.class);
        startActivity(intent);
        WelcomeActivity.this.finish();
    }
}